/**
* @file main.cpp
* @author Clemens Zangl
* @date 22.05.2012
* @version 0.1
*
* (c) 2011 2011 STMicroelectronics
* (c) 2012 University of Applied Sciences Karlsruhe
*
* Definition to main.h
** 
*/

#define USARTPORT 2
#define USARTRATE 9600

#include "main.h"
#include "misc/misc_timing.h"
#include "misc/misc_globals.h"
#include "interfaces/spi/interface_spi.h"
#include "interfaces/adc/interface_adc.h"
#include "interfaces/usart/interface_usart.h"
#include "stm32f4xx_adc.h"


using namespace HardwareInterface;


static void TIM_Config(void);




/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
	
int main(void)
{
	uint8_t ctrl = 0;

	int ADCvalue = 0;
	int16_t v25= 1075;
	int temperature = 0;
	
	AnalogDigitalConverter Temperatursensor(ADC_Channel_TempSensor,ADC_SampleTime_112Cycles);	

	//create UART 1 on PINS 9 + 10 for serial communication
	ConnectionUSART Port1(USARTPORT, USARTRATE);
	
	LIS302DL_InitTypeDef  LIS302DL_InitStruct;
	LIS302DL_InterruptConfigTypeDef LIS302DL_InterruptStruct;  
	
	// SysTick end of count event each 10ms 
	SysTick_Config(SystemCoreClock / 100);
	
	// Set configuration of LIS302DL
	LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
	LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
	LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE;
	LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
	LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
	
	
	spi = new SPIInterface(deviceSPIOne, configurationSPIMaster);
	
		LIS302DL_Init(spi, &LIS302DL_InitStruct);
	
	// Set configuration of Internal High Pass Filter of LIS302DL
	LIS302DL_InterruptStruct.Latch_Request = LIS302DL_INTERRUPTREQUEST_LATCHED;
	LIS302DL_InterruptStruct.SingleClick_Axes = LIS302DL_CLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptStruct.DoubleClick_Axes = LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE;
	LIS302DL_InterruptConfig(spi, &LIS302DL_InterruptStruct);
	
	// Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate = 3/100 = 30ms 
	Delay(30);
	
	// Configure Interrupt control register: enable Click interrupt1 
	ctrl = 0x07;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CTRL_REG3_ADDR, 1);
	
	// Enable Interrupt generation on click/double click on Z axis 
	ctrl = 0x40;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_CFG_REG_ADDR, 1);
	
	// Configure Click Threshold on X/Y axis (10 x 0.5g) 
	ctrl = 0xAA;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_THSY_X_REG_ADDR, 1);
	
	// Configure Click Threshold on Z axis (10 x 0.5g) 
	ctrl = 0x0A;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_THSZ_REG_ADDR, 1);
	
	// Configure Time Limit 
	ctrl = 0x03;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_TIMELIMIT_REG_ADDR, 1);
	
	// Configure Latency 
	ctrl = 0x7F;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_LATENCY_REG_ADDR, 1);
	
	// Configure Click Window 
	ctrl = 0x7F;
	LIS302DL_Write(spi, &ctrl, LIS302DL_CLICK_WINDOW_REG_ADDR, 1);
	
	// TIM configuration -------------------------------------------------------
	TIM_Config(); 
	
	{
		uint8_t Buffer[6];
		LIS302DL_Read(spi, Buffer, LIS302DL_OUT_X_ADDR, 6);
									
		XOffset = Buffer[0];
		YOffset = Buffer[2];
	}
	
  while(1)
  {
		
		  //read data from temperature sensor on ADC16
			ADCvalue = Temperatursensor.readValue();

			//send raw data got from ADC16	over USART
			Port1.SendData("ADC-Raw Wert: ");
			Port1.SendData((int)ADCvalue);
			 
			//convert measured adc value to celsius degrees	
			temperature=(int)(((double)(ADCvalue - v25)/4096.0*3.3/0.0025)+25.0);

			//send failure if temperature is below 0
			if(temperature < 0)
			{
				Port1.SendData("Wert ist negativ!\n");
			}

			//send temperature over USART
			Port1.SendData("Messwert in Grad Celius: ");
			Port1.SendData((int)temperature);
			
  }
}


/**
  * @brief  Configures the TIM Peripheral.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
	uint16_t PrescalerValue = 0;
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  // --------------------------- System Clocks Configuration -----------------//
  // TIM4 clock enable 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  
  /* GPIOD clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  
  //-------------------------- GPIO Configuration ----------------------------//
	
	
  /*
	Set up the configuration for the LEDS.
	GPIOD Configuration: Pins 12, 13, 14 and 15 in output push-pull 
	Pin12 Green
	Pin13 Orange
	Pin14 Red 
	Pin15 Blue
	*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  // Connect TIM4 pins to the LEDs  
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4); 
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4); 
  
   
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 2000) - 1;
  
  // Set up the Timer for the LEDs
  TIM_TimeBaseStructure.TIM_Period = TIM_ARR;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  
  // Enable TIM4 Preload register on ARR
  TIM_ARRPreloadConfig(TIM4, ENABLE);
  
  // TIM PWM1 Mode configuration: Channel
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = TIM_CCR;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  
  // Output Compare PWM1 Mode configuration: Channel1
  TIM_OC1Init(TIM4, &TIM_OCInitStructure);
  TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE);
  
  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
  
  // Output Compare PWM1 Mode configuration: Channel2 
  TIM_OC2Init(TIM4, &TIM_OCInitStructure);
  TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE);
  
  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
    
  // Output Compare PWM1 Mode configuration: Channel3 
  TIM_OC3Init(TIM4, &TIM_OCInitStructure);
  TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE);
  
  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
  
  // Output Compare PWM1 Mode configuration: Channel4 
  TIM_OC4Init(TIM4, &TIM_OCInitStructure);
  TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE);
  
  TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
  
  // TIM4 enable counter
  TIM_Cmd(TIM4, ENABLE);
}



/**
  * @brief  MEMS accelerometre management of the timeout situation.
  * @param  None.
  * @retval None.
  */
uint32_t LIS302DL_TIMEOUT_UserCallback(void)
{
  // MEMS error (timeout)
  while (1)
  {   
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
